<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<title>Coverage for c:\program files\python26\lib\site-packages\modbus_tk-0.3-py2.6.egg\modbus_tk\utils</title>
<link rel='stylesheet' href='style.css' type='text/css'>
<script type='text/javascript' src='jquery-1.3.2.min.js'></script>
<script type='text/javascript'>
function toggle_lines(btn, cls) {
    var btn = $(btn);
    var hide = "hide_"+cls;
    if (btn.hasClass(hide)) {
        $("#source ."+cls).removeClass(hide);
        btn.removeClass(hide);
    }
    else {
        $("#source ."+cls).addClass(hide);
        btn.addClass(hide);
    }
}
</script>
</head>
<body>
<div id='header'>
    <div class='content'>
        <h1>Coverage for <b>c:\program files\python26\lib\site-packages\modbus_tk-0.3-py2.6.egg\modbus_tk\utils</b> :
            <span class='pc_cov'>53%</span>
        </h1>
        <h2 class='stats'>
            101 statements
            <span class='run hide_run' onclick='toggle_lines(this, "run")'>62 run</span>
            <span class='exc' onclick='toggle_lines(this, "exc")'>0 excluded</span>
            <span class='mis' onclick='toggle_lines(this, "mis")'>39 missing</span>
            
                <span class='par run hide_run' onclick='toggle_lines(this, "par")'>4 partial</span>
            
        </h2>
    </div>
</div>

<div id='source'>
<table cellspacing='0' cellpadding='0'>
<tr>
<td class='linenos' valign='top'>
<p class='pln'>1</p>
<p class='pln'>2</p>
<p class='stm run hide_run'>3</p>
<p class='pln'>4</p>
<p class='pln'>5</p>
<p class='pln'>6</p>
<p class='pln'>7</p>
<p class='pln'>8</p>
<p class='pln'>9</p>
<p class='pln'>10</p>
<p class='pln'>11</p>
<p class='stm run hide_run'>12</p>
<p class='stm run hide_run'>13</p>
<p class='stm run hide_run'>14</p>
<p class='stm run hide_run'>15</p>
<p class='pln'>16</p>
<p class='stm run hide_run'>17</p>
<p class='pln'>18</p>
<p class='stm run hide_run'>19</p>
<p class='stm run hide_run'>20</p>
<p class='pln'>21</p>
<p class='stm run hide_run'>22</p>
<p class='stm run hide_run'>23</p>
<p class='stm run hide_run'>24</p>
<p class='stm run hide_run'>25</p>
<p class='stm run hide_run'>26</p>
<p class='pln'>27</p>
<p class='stm run hide_run'>28</p>
<p class='stm run hide_run'>29</p>
<p class='stm run hide_run'>30</p>
<p class='pln'>31</p>
<p class='stm run hide_run'>32</p>
<p class='pln'>33</p>
<p class='stm run hide_run'>34</p>
<p class='stm par run hide_run'>35</p>
<p class='stm run hide_run'>36</p>
<p class='stm par run hide_run'>37</p>
<p class='stm run hide_run'>38</p>
<p class='stm mis'>39</p>
<p class='stm mis'>40</p>
<p class='pln'>41</p>
<p class='stm run hide_run'>42</p>
<p class='pln'>43</p>
<p class='stm run hide_run'>44</p>
<p class='stm run hide_run'>45</p>
<p class='stm run hide_run'>46</p>
<p class='stm run hide_run'>47</p>
<p class='pln'>48</p>
<p class='stm run hide_run'>49</p>
<p class='pln'>50</p>
<p class='pln'>51</p>
<p class='stm run hide_run'>52</p>
<p class='pln'>53</p>
<p class='stm mis'>54</p>
<p class='pln'>55</p>
<p class='stm run hide_run'>56</p>
<p class='pln'>57</p>
<p class='stm mis'>58</p>
<p class='pln'>59</p>
<p class='stm run hide_run'>60</p>
<p class='pln'>61</p>
<p class='pln'>62</p>
<p class='stm run hide_run'>63</p>
<p class='pln'>64</p>
<p class='stm run hide_run'>65</p>
<p class='stm run hide_run'>66</p>
<p class='stm run hide_run'>67</p>
<p class='pln'>68</p>
<p class='stm run hide_run'>69</p>
<p class='pln'>70</p>
<p class='stm run hide_run'>71</p>
<p class='pln'>72</p>
<p class='stm run hide_run'>73</p>
<p class='pln'>74</p>
<p class='pln'>75</p>
<p class='stm run hide_run'>76</p>
<p class='pln'>77</p>
<p class='stm run hide_run'>78</p>
<p class='pln'>79</p>
<p class='stm run hide_run'>80</p>
<p class='pln'>81</p>
<p class='stm run hide_run'>82</p>
<p class='pln'>83</p>
<p class='stm run hide_run'>84</p>
<p class='pln'>85</p>
<p class='pln'>86</p>
<p class='stm run hide_run'>87</p>
<p class='stm run hide_run'>88</p>
<p class='stm run hide_run'>89</p>
<p class='stm run hide_run'>90</p>
<p class='stm run hide_run'>91</p>
<p class='stm par run hide_run'>92</p>
<p class='stm mis'>93</p>
<p class='stm par run hide_run'>94</p>
<p class='stm run hide_run'>95</p>
<p class='pln'>96</p>
<p class='stm mis'>97</p>
<p class='stm run hide_run'>98</p>
<p class='stm run hide_run'>99</p>
<p class='stm run hide_run'>100</p>
<p class='pln'>101</p>
<p class='stm run hide_run'>102</p>
<p class='pln'>103</p>
<p class='stm mis'>104</p>
<p class='stm mis'>105</p>
<p class='stm mis'>106</p>
<p class='pln'>107</p>
<p class='stm run hide_run'>108</p>
<p class='pln'>109</p>
<p class='stm mis'>110</p>
<p class='stm mis'>111</p>
<p class='stm mis'>112</p>
<p class='stm mis'>113</p>
<p class='stm mis'>114</p>
<p class='stm mis'>115</p>
<p class='stm mis'>116</p>
<p class='stm mis'>117</p>
<p class='stm mis'>118</p>
<p class='pln'>119</p>
<p class='stm run hide_run'>120</p>
<p class='pln'>121</p>
<p class='stm mis'>122</p>
<p class='stm mis'>123</p>
<p class='pln'>124</p>
<p class='stm mis'>125</p>
<p class='pln'>126</p>
<p class='stm run hide_run'>127</p>
<p class='pln'>128</p>
<p class='pln'>129</p>
<p class='pln'>130</p>
<p class='pln'>131</p>
<p class='stm run hide_run'>132</p>
<p class='pln'>133</p>
<p class='stm mis'>134</p>
<p class='stm mis'>135</p>
<p class='stm mis'>136</p>
<p class='stm mis'>137</p>
<p class='pln'>138</p>
<p class='stm run hide_run'>139</p>
<p class='pln'>140</p>
<p class='stm mis'>141</p>
<p class='stm mis'>142</p>
<p class='pln'>143</p>
<p class='stm run hide_run'>144</p>
<p class='pln'>145</p>
<p class='stm mis'>146</p>
<p class='stm mis'>147</p>
<p class='stm mis'>148</p>
<p class='pln'>149</p>
<p class='stm run hide_run'>150</p>
<p class='pln'>151</p>
<p class='stm mis'>152</p>
<p class='stm mis'>153</p>
<p class='stm mis'>154</p>
<p class='stm mis'>155</p>
<p class='stm mis'>156</p>
<p class='stm mis'>157</p>
<p class='stm mis'>158</p>
<p class='pln'>159</p>
<p class='stm mis'>160</p>
    
</td>
<td class='text' valign='top'>
<p class='pln'><span class='com'>#!/usr/bin/env python</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='com'># -*- coding: utf-8 -*-</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='str'>&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'> Modbus TestKit: Implementation of Modbus protocol in python</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'></span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'> (C)2009 - Luc Jean - luc.jean@gmail.com</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'> (C)2009 - Apidev - http://www.apidev.fr</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'></span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'> This is distributed under GNU LGPL license, see license.txt</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>import</span> <span class='nam'>threading</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>import</span> <span class='nam'>logging</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>import</span> <span class='nam'>socket</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>import</span> <span class='nam'>select</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>def</span> <span class='nam'>threadsafe_function</span><span class='op'>(</span><span class='nam'>fcn</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;decorator making sure that the decorated function is thread safe&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='nam'>lock</span> <span class='op'>=</span> <span class='nam'>threading</span><span class='op'>.</span><span class='nam'>Lock</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>new</span><span class='op'>(</span><span class='op'>*</span><span class='nam'>args</span><span class='op'>,</span> <span class='op'>**</span><span class='nam'>kwargs</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;lock and call the decorated function&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>lock</span><span class='op'>.</span><span class='nam'>acquire</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>try</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>ret</span> <span class='op'>=</span> <span class='nam'>fcn</span><span class='op'>(</span><span class='op'>*</span><span class='nam'>args</span><span class='op'>,</span> <span class='op'>**</span><span class='nam'>kwargs</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>except</span> <span class='nam'>Exception</span><span class='op'>,</span> <span class='nam'>excpt</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>excpt</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>finally</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>lock</span><span class='op'>.</span><span class='nam'>release</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>ret</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>new</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>def</span> <span class='nam'>flush_socket</span><span class='op'>(</span><span class='nam'>socks</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;remove the data present on the socket&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='nam'>input_socks</span> <span class='op'>=</span> <span class='op'>[</span><span class='nam'>socks</span><span class='op'>]</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>exit</span>&nbsp; &nbsp; <span class='key'>while</span> <span class='num'>1</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>i_socks</span><span class='op'>,</span> <span class='nam'>o_socks</span><span class='op'>,</span> <span class='nam'>e_socks</span> <span class='op'>=</span> <span class='nam'>select</span><span class='op'>.</span><span class='nam'>select</span><span class='op'>(</span><span class='nam'>input_socks</span><span class='op'>,</span> <span class='nam'>input_socks</span><span class='op'>,</span> <span class='nam'>input_socks</span><span class='op'>,</span> <span class='num'>0.0</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>39</span>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>len</span><span class='op'>(</span><span class='nam'>i_socks</span><span class='op'>)</span><span class='op'>==</span><span class='num'>0</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>break</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>for</span> <span class='nam'>sock</span> <span class='key'>in</span> <span class='nam'>i_socks</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>sock</span><span class='op'>.</span><span class='nam'>recv</span><span class='op'>(</span><span class='num'>1024</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>def</span> <span class='nam'>get_log_buffer</span><span class='op'>(</span><span class='nam'>prefix</span><span class='op'>,</span> <span class='nam'>buff</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Format binary data into a string for debug purpose&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='nam'>log</span> <span class='op'>=</span> <span class='nam'>prefix</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>for</span> <span class='nam'>i</span> <span class='key'>in</span> <span class='nam'>buff</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>log</span> <span class='op'>+=</span> <span class='nam'>str</span><span class='op'>(</span><span class='nam'>ord</span><span class='op'>(</span><span class='nam'>i</span><span class='op'>)</span><span class='op'>)</span> <span class='op'>+</span> <span class='str'>&quot;-&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>log</span><span class='op'>[</span><span class='op'>:</span><span class='op'>-</span><span class='num'>1</span><span class='op'>]</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>class</span> <span class='nam'>ConsoleHandler</span><span class='op'>(</span><span class='nam'>logging</span><span class='op'>.</span><span class='nam'>Handler</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;This class is a logger handler. It prints on the console&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>__init__</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Constructor&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>logging</span><span class='op'>.</span><span class='nam'>Handler</span><span class='op'>.</span><span class='nam'>__init__</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>emit</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>record</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;format and print the record on the console&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>print</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>format</span><span class='op'>(</span><span class='nam'>record</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>class</span> <span class='nam'>LogitHandler</span><span class='op'>(</span><span class='nam'>logging</span><span class='op'>.</span><span class='nam'>Handler</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;This class is a logger handler. It send to a udp socket&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>__init__</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>dest</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Constructor&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>logging</span><span class='op'>.</span><span class='nam'>Handler</span><span class='op'>.</span><span class='nam'>__init__</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_dest</span> <span class='op'>=</span> <span class='nam'>dest</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_sock</span> <span class='op'>=</span> <span class='nam'>socket</span><span class='op'>.</span><span class='nam'>socket</span><span class='op'>(</span><span class='nam'>socket</span><span class='op'>.</span><span class='nam'>AF_INET</span><span class='op'>,</span> <span class='nam'>socket</span><span class='op'>.</span><span class='nam'>SOCK_DGRAM</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>emit</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>record</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;format and send the record over udp&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_sock</span><span class='op'>.</span><span class='nam'>sendto</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>.</span><span class='nam'>format</span><span class='op'>(</span><span class='nam'>record</span><span class='op'>)</span><span class='op'>+</span><span class='str'>&quot;\r\n&quot;</span><span class='op'>,</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_dest</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>class</span> <span class='nam'>DummyHandler</span><span class='op'>(</span><span class='nam'>logging</span><span class='op'>.</span><span class='nam'>Handler</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;This class is a logger handler. It doesn&#39;t do anything&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>__init__</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Constructor&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>logging</span><span class='op'>.</span><span class='nam'>Handler</span><span class='op'>.</span><span class='nam'>__init__</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>emit</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>record</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;do nothinbg with the given record&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>pass</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='key'>def</span> <span class='nam'>create_logger</span><span class='op'>(</span><span class='nam'>name</span><span class='op'>=</span><span class='str'>&quot;dummy&quot;</span><span class='op'>,</span> <span class='nam'>level</span><span class='op'>=</span><span class='nam'>logging</span><span class='op'>.</span><span class='nam'>DEBUG</span><span class='op'>,</span><span class='nam'>record_format</span><span class='op'>=</span><span class='str'>&quot;%(asctime)s\t%(levelname)s\t%(module)s.%(funcName)s\t%(threadName)s\t%(message)s&quot;</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Create a logger according to the given settings&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='nam'>logger</span> <span class='op'>=</span> <span class='nam'>logging</span><span class='op'>.</span><span class='nam'>getLogger</span><span class='op'>(</span><span class='str'>&quot;modbus_tk&quot;</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='nam'>logger</span><span class='op'>.</span><span class='nam'>setLevel</span><span class='op'>(</span><span class='nam'>level</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='nam'>formatter</span> <span class='op'>=</span> <span class='nam'>logging</span><span class='op'>.</span><span class='nam'>Formatter</span><span class='op'>(</span><span class='nam'>record_format</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>name</span> <span class='op'>==</span> <span class='str'>&quot;udp&quot;</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>log_handler</span> <span class='op'>=</span> <span class='nam'>LogitHandler</span><span class='op'>(</span><span class='op'>(</span><span class='str'>&quot;127.0.0.1&quot;</span><span class='op'>,</span> <span class='num'>1975</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>elif</span> <span class='nam'>name</span> <span class='op'>==</span> <span class='str'>&quot;console&quot;</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>93</span>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>log_handler</span> <span class='op'>=</span> <span class='nam'>ConsoleHandler</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; <span class='key'>elif</span> <span class='nam'>name</span> <span class='op'>==</span> <span class='str'>&quot;dummy&quot;</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm par run hide_run'><span class='annotate' title='no jump to this line number'>97</span>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>log_handler</span> <span class='op'>=</span> <span class='nam'>DummyHandler</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>else</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>raise</span> <span class='nam'>Exception</span><span class='op'>(</span><span class='str'>&quot;Unknown handler %s&quot;</span> <span class='op'>%</span> <span class='nam'>name</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; <span class='nam'>log_handler</span><span class='op'>.</span><span class='nam'>setFormatter</span><span class='op'>(</span><span class='nam'>formatter</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='nam'>logger</span><span class='op'>.</span><span class='nam'>addHandler</span><span class='op'>(</span><span class='nam'>log_handler</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>logger</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='key'>def</span> <span class='nam'>swap_bytes</span><span class='op'>(</span><span class='nam'>word_val</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;swap lsb and msb of a word&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='nam'>msb</span> <span class='op'>=</span> <span class='nam'>word_val</span> <span class='op'>&gt;&gt;</span> <span class='num'>8</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; <span class='nam'>lsb</span> <span class='op'>=</span> <span class='nam'>word_val</span> <span class='op'>%</span> <span class='num'>256</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; <span class='key'>return</span> <span class='op'>(</span><span class='nam'>lsb</span> <span class='op'>&lt;&lt;</span> <span class='num'>8</span><span class='op'>)</span> <span class='op'>+</span> <span class='nam'>msb</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='key'>def</span> <span class='nam'>calculate_crc</span><span class='op'>(</span><span class='nam'>data</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Calculate the CRC16 of a datagram&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='nam'>crc</span> <span class='op'>=</span> <span class='num'>0xFFFF</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; <span class='key'>for</span> <span class='nam'>i</span> <span class='key'>in</span> <span class='nam'>data</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>crc</span> <span class='op'>=</span> <span class='nam'>crc</span> <span class='op'>^</span> <span class='nam'>ord</span><span class='op'>(</span><span class='nam'>i</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>for</span> <span class='nam'>j</span> <span class='key'>in</span> <span class='nam'>xrange</span><span class='op'>(</span><span class='num'>8</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>tmp</span> <span class='op'>=</span> <span class='nam'>crc</span> <span class='op'>&amp;</span> <span class='num'>1</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>crc</span> <span class='op'>=</span> <span class='nam'>crc</span> <span class='op'>&gt;&gt;</span> <span class='num'>1</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>tmp</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>crc</span> <span class='op'>=</span> <span class='nam'>crc</span> <span class='op'>^</span> <span class='num'>0xA001</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; <span class='key'>return</span> <span class='nam'>swap_bytes</span><span class='op'>(</span><span class='nam'>crc</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='key'>def</span> <span class='nam'>calculate_rtu_inter_char</span><span class='op'>(</span><span class='nam'>baudrate</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;calculates the interchar delay from the baudrate&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>baudrate</span> <span class='op'>&lt;=</span> <span class='num'>19200</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='num'>11.0</span> <span class='op'>/</span> <span class='nam'>baudrate</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; <span class='key'>else</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>return</span> <span class='num'>0.0005</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='key'>class</span> <span class='nam'>WorkerThread</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; A thread which is running an almost-ever loop</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; It can be stopped by calling the stop function</span><span class='strut'>&nbsp;</span></p>
<p class='pln'><span class='str'>&nbsp; &nbsp; &quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>__init__</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span> <span class='nam'>main_fct</span><span class='op'>,</span> <span class='nam'>args</span><span class='op'>=</span><span class='op'>(</span><span class='op'>)</span><span class='op'>,</span> <span class='nam'>init_fct</span><span class='op'>=</span><span class='nam'>None</span><span class='op'>,</span> <span class='nam'>exit_fct</span><span class='op'>=</span><span class='nam'>None</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Constructor&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_fcts</span> <span class='op'>=</span> <span class='op'>[</span><span class='nam'>init_fct</span><span class='op'>,</span> <span class='nam'>main_fct</span><span class='op'>,</span> <span class='nam'>exit_fct</span><span class='op'>]</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_args</span> <span class='op'>=</span> <span class='nam'>args</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_thread</span> <span class='op'>=</span> <span class='nam'>threading</span><span class='op'>.</span><span class='nam'>Thread</span><span class='op'>(</span><span class='nam'>target</span><span class='op'>=</span><span class='nam'>WorkerThread</span><span class='op'>.</span><span class='nam'>_run</span><span class='op'>,</span> <span class='nam'>args</span><span class='op'>=</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>,</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_go</span> <span class='op'>=</span> <span class='nam'>threading</span><span class='op'>.</span><span class='nam'>Event</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>start</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;Start the thread&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_go</span><span class='op'>.</span><span class='nam'>set</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_thread</span><span class='op'>.</span><span class='nam'>start</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>stop</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;stop the thread&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_thread</span><span class='op'>.</span><span class='nam'>isAlive</span><span class='op'>(</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_go</span><span class='op'>.</span><span class='nam'>clear</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_thread</span><span class='op'>.</span><span class='nam'>join</span><span class='op'>(</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; <span class='key'>def</span> <span class='nam'>_run</span><span class='op'>(</span><span class='nam'>self</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm run hide_run'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='str'>&quot;&quot;&quot;main function of the thread execute _main_fct until stop is called&quot;&quot;&quot;</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>try</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_fcts</span><span class='op'>[</span><span class='num'>0</span><span class='op'>]</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_fcts</span><span class='op'>[</span><span class='num'>0</span><span class='op'>]</span><span class='op'>(</span><span class='op'>*</span><span class='nam'>self</span><span class='op'>.</span><span class='nam'>_args</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>while</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_go</span><span class='op'>.</span><span class='nam'>isSet</span><span class='op'>(</span><span class='op'>)</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_fcts</span><span class='op'>[</span><span class='num'>1</span><span class='op'>]</span><span class='op'>(</span><span class='op'>*</span><span class='nam'>self</span><span class='op'>.</span><span class='nam'>_args</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>except</span> <span class='nam'>Exception</span><span class='op'>,</span> <span class='nam'>excpt</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>LOGGER</span><span class='op'>.</span><span class='nam'>error</span><span class='op'>(</span><span class='str'>&quot;error: %s&quot;</span> <span class='op'>%</span> <span class='nam'>str</span><span class='op'>(</span><span class='nam'>excpt</span><span class='op'>)</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>finally</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='pln'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='key'>if</span> <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_fcts</span><span class='op'>[</span><span class='num'>2</span><span class='op'>]</span><span class='op'>:</span><span class='strut'>&nbsp;</span></p>
<p class='stm mis'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class='nam'>self</span><span class='op'>.</span><span class='nam'>_fcts</span><span class='op'>[</span><span class='num'>2</span><span class='op'>]</span><span class='op'>(</span><span class='op'>*</span><span class='nam'>self</span><span class='op'>.</span><span class='nam'>_args</span><span class='op'>)</span><span class='strut'>&nbsp;</span></p>
    
</td>
</tr>
</table>
</div>

</body>
</html>
